package inline.core;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

public class ProductsInBasketDbAdapter {

	private static final String LOG_TAG = "ProductsDbAdapter";

	public static final String KEY_PRODUCT_ID = "product_id";
	public static final String KEY_AMOUNT = "amount";
	public static final String KEY_ROWID = "_id";

	private static final String DATABASE_TABLE = "productsbasket";
	/**
	 * Database creation sql statement
	 */
	static final String DATABASE_CREATE = "create table " + DATABASE_TABLE
			+ "  (" + KEY_ROWID + " integer primary key autoincrement, "
			+ KEY_PRODUCT_ID + " integer not null, " + KEY_AMOUNT
			+ " integer not null " + ");";

	private SQLiteDatabase mDb;

	public ProductsInBasketDbAdapter(SQLiteDatabase mDb) {
		if (mDb == null)
			throw new IllegalArgumentException();
		this.mDb = mDb;
	}

	public long addToBasket(int productId, int amount) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_PRODUCT_ID, productId);
		initialValues.put(KEY_AMOUNT, amount);
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}

	public boolean clearBakset() {
		return mDb.delete(DATABASE_TABLE, null, null) > 0;
	}

	/*
	 * public Cursor fetchBasket() { return mDb.query(DATABASE_TABLE, new
	 * String[] { KEY_ROWID, KEY_PRODUCT_ID, KEY_AMOUNT }, null, null, null,
	 * null, null); }
	 */

	public Cursor fetchBasket() {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		String tables = ProductsInBasketDbAdapter.DATABASE_TABLE + " JOIN "
				+ ProductsDbAdapter.DATABASE_TABLE + " ON ("
				+ ProductsInBasketDbAdapter.DATABASE_TABLE + "."
				+ ProductsInBasketDbAdapter.KEY_PRODUCT_ID + " = "
				+ ProductsDbAdapter.DATABASE_TABLE + "."
				+ ProductsDbAdapter.KEY_ROWID + ") ";
		queryBuilder.setTables(tables);
		Log.i(LOG_TAG, "tables: " + tables);
		String[] projectionIn = new String[] {
				ProductsInBasketDbAdapter.DATABASE_TABLE + "." + KEY_ROWID,
				ProductsDbAdapter.KEY_NAME, ProductsDbAdapter.KEY_UNIT_PRICE,
				ProductsInBasketDbAdapter.KEY_AMOUNT };
		for (int i = 0; i < projectionIn.length; i++) {
			Log.i(LOG_TAG, "projectionIn[" + i + "]: " + projectionIn[i]);
		}
		String selection = null;
		String[] selectionArgs = null;
		String groupBy = null;
		String having = null;
		String sortOrder = null;
		Cursor cursor = queryBuilder.query(mDb, projectionIn, selection,
				selectionArgs, groupBy, having, sortOrder);
		/*
		 * String[] columnNames = cursor.getColumnNames(); for (int i = 0; i <
		 * columnNames.length; i++) { String string = columnNames[i];
		 * Log.i(LOG_TAG,"columnNames["+i+"] " + string); }
		 * 
		 * for (cursor.first();!cursor.isAfterLast();){ String productName =
		 * cursor.getString(cursor.getColumnIndex(ProductsDbAdapter.KEY_NAME));
		 * Log.i(LOG_TAG,"productName: " + productName); cursor.next(); }
		 */

		return cursor;
	}

	public int fetchBasketTotal() {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		String tables = ProductsInBasketDbAdapter.DATABASE_TABLE + " JOIN "
				+ ProductsDbAdapter.DATABASE_TABLE + " ON ("
				+ ProductsInBasketDbAdapter.DATABASE_TABLE + "."
				+ ProductsInBasketDbAdapter.KEY_PRODUCT_ID + " = "
				+ ProductsDbAdapter.DATABASE_TABLE + "."
				+ ProductsDbAdapter.KEY_ROWID + ") ";
		queryBuilder.setTables(tables);
		String sumProjection = " SUM("
				+ ProductsDbAdapter.KEY_UNIT_PRICE + ")";
		String[] projectionIn = new String[] { sumProjection };
		String selection = null;
		String[] selectionArgs = null;
		String groupBy = null;
		String having = null;
		String sortOrder = null;
		Cursor cursor = queryBuilder.query(mDb, projectionIn, selection,
				selectionArgs, groupBy, having, sortOrder);
		cursor.first();
		String[] columnNames = cursor.getColumnNames();
		for (int i = 0; i < columnNames.length; i++) {
			String string = columnNames[i];
			Log.i(LOG_TAG, "columnNames[" + i + "] " + string);
		}
		int result = cursor.getInt(0);
		return result;
	}
}
